/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');
var PokerRoom = require('../Poker/room').PokerRoom;

var Const = require('./const');
var SubCode = require('../subCode');

var RoundStatus = {
    'WAITING': 1,
    'PREPARE': 2,
    'READY': 3,
};

exports.LXELFDDZRoom = PokerRoom.extend({
    _roundStatus: RoundStatus.WAITING,
    _curCardsInfo : {},
    _curPlay: 1,
    _curStatus: 0,
    _catCards: 0,
    _diZhuCards: [],
    _diZhuWin: false,
    _bombNum: 0,
    _roundTurns: 0,
    _roundRate: 0,
    _spring: false,
    _diZhu: 1,
    init: function(rid, creator, msg) {
        this._super(rid, creator, msg);
        this._curCardsInfo = {
            'pattern': 0,
            'cards': {},
            'playerIndex': 0,
            'maxCard': 0,
            'cardsNum': 0,
            'lastPlayCards': {}
        };
        this._curPlay = 1;
        this._diZhu = 0;
        this._diZhuCards = [];
        this._roundTurns = 0;
        this._catCards = 0;
        this._spring = false;
        this._diZhuWin = false;
        this._curStatus = 0;          //0没有,1叫地主，2踢，3跟踢，4回踢，5出牌
        this._roundStatus = RoundStatus.WAITING;
        this._dealerIndex = 1;
        this._bombNum = 0;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getRoundInfo: function() {
        var info = this._super();
        info.selectWaitMS = this.getHideOption("selectWaitMS");
        return info;
    },
    getRoomConfig: function (){
        var cfg = this._super();
        cfg.respDestroyIgnore = 1;
        return cfg;
    },
    getCurRate: function(){
        var pow = this._bombNum;
        var maxRate = this.getOption("maxRate");
        if(maxRate){
            if(pow > maxRate){
                pow = maxRate;
            }
        }
        return Math.pow(2, pow);
    },
    getReconnectInfo: function(rePlayer) {
        var info = this._super(rePlayer);
        info.curPlay = this._curPlay;
        //info.roundCardInfo = clone(this._curCardsInfo);
        //info.roundCardInfo.maxCard = 0;
        info.lastPlayCards = this._curCardsInfo.lastPlayCards;
        info.lastPlayerIndex = this._curCardsInfo.playerIndex;
        info.curRate = (this._bombNum + 1);
        info.turns = this._roundTurns;
        info.diZhu = this._diZhu;
        info.jiaoScore = this._roundRate;
        info.roomStatus = this._curStatus;
        if(this.getSubType() == 'LFDDZ'){
            if(rePlayer.getIndex() == this._diZhu){
                info.holeCards = this._diZhuCards;
            }else {
                info.holeCards = [];
            }
        }else {
            info.holeCards = this._diZhuCards;
        }

        //info.publicCardNum = this._publicCards.getIndex;
        /*info.dealerLZ = this._dealerLZ;
        info.publicCardNum = this._publicCards.getRemainNum();*/
        return info;
    },
    getInfo: function() {
        var info = this._super();
        info.roomStatus = this._curStatus;
        info.diZhu = this._diZhu;

        return info;
    },
    onBeforeStartNewRound: function() {

        this._super();
        this._spring = false;
        this._bombNum = 0;
        this._diZhuWin = false;
        this._curStatus = Const.Status.NONE;
        this._diZhuCards = [];
        this._roundRate = 0;

    },

    onAfterStartNewRound: function() {
        this._super();
        if(!this._catCards){
            this._preStart();
        }
    },
    _preStart: function(){
        this.initPlayersHandCards();
        this._curStatus = Const.Status.JIAO;
        console.log("diz" + this._dealerIndex);
        this._changeCurPlay(this._dealerIndex);
        //this.startPlay();
    },
    startPlay: function(){
        this._curCardsInfo.playerIndex = this._diZhu;

        this.enumPlaying(function(eIndex, ePlayer){
            ePlayer._jiaoStatus = Const.PlayerStatus.NONE;
        }.bind(this));
        this.broadcast("onStartPlayCard", {});
        this._changeCurPlay(this._diZhu);
    },


    onPlayerJiaoDiZhu: function(){
        this.diZhuCards();
        var farmer1 = this.getPlayerByIndex(this.getNextIndex(this._diZhu));
        var farmer2 = this.getPlayerByIndex(this.getNextIndex(farmer1.getIndex()));
        farmer1._partner = farmer2.getIndex();
        farmer2._partner = farmer1.getIndex();
        logger.debug("room=%d onPlayerJiaoDiZhu dizhu = %d ,farmer1 = %d, farmer2 = %d", this.rid(), this._diZhu, farmer1.getIndex(), farmer2.getIndex());
    },
    diZhuCards: function(){
        var player = this.getPlayerByIndex(this._diZhu);
        var subConfig = this.getSubConfig();
        var remain = this._publicCards.getRemainNum();
        var sendCards = [];
        //发地主底牌
        for(var i = 0; i < remain; i++){
            var card = this._publicCards.getCard();
            sendCards.push(card);
            player.addNewCard(card);
        }
        this._diZhuCards = sendCards;

        this.enumPlaying(function(eIndex, ePlayer){
            var msg = {
                'cards': [],
                'playerIndex': this._diZhu,
                'num': sendCards.length
            };
            if(this.getSubType() == 'LFDDZ'){
                if(eIndex == this._diZhu){
                    msg.cards = sendCards;
                }
            }else {
                msg.cards = sendCards;
            }

            ePlayer.send("onDiZhuCards", msg);
        }.bind(this));

    },
    handlePass: function(player, msg, callback){
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING}); return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING}); return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY}); return;
        }
        if(player._pass == false){
            callback({code: SubCode.LXE_CANT_PASS}); return;
        }
        callback({});
        var MSG = {
            'playerIndex' : 0,
        };
        MSG.playerIndex = player.getIndex();
        this.broadcast("onPlayerPass", MSG);
        player._pass == true;
        if(this._curStatus == Const.Status.JIAO){
            player._tiStat = 0;
            var next = this.getNextPlaying(this._curPlay);
            if(next == this._dealerIndex){
                this._doStartNewRound();
                return;
            }
            this._changeCurPlay();
        }else if(this._curStatus == Const.Status.TI){
            player._tiStat = 0;
            player._tiCard = 0;
            var partner = this.getPlayerByIndex(player._partner);
            if(partner._tiStat < 0){
                this._curStatus = Const.Status.TI;
                this._changeCurPlay(partner.getIndex());
            }else{
                this._curStatus = Const.Status.CHU;
                this.startPlay();
            }
        }else if(this._curStatus == Const.Status.GEN){
            player._tiStat = 0;
            player._tiCard = 0;
            this._curStatus = Const.Status.HUI;
            this._changeCurPlay(this._diZhu);
        }else if(this._curStatus == Const.Status.HUI){
            player._tiStat = 0;
            player._tiCard = 0;
            this._curStatus = Const.Status.CHU;
            this.startPlay();
        }else if(this._curStatus == Const.Status.CHU){
            player._lastCards = {
                pass: 0
            };
            player._jiaoStatus = Const.PlayerStatus.BUCHU;
            this.broadcast("onPlayerStatusChange",{
                playerIndex: player.getIndex(),
                status: player._jiaoStatus
            });
            this._changeCurPlay();
        }

    },
    /*handleGift: function (player, msg, callback) {
        if (isNaN(msg.targetIndex)) {
            callback({code: Code.ARGS_INVALID});
            return;
        }

        var gameConfig = this.getGameConfig();
        var giftType = msg.type;
        var giftConfig = gameConfig.Gift[giftType];
        if (!giftConfig) {
            callback({code: Code.ARGS_VALUE_WRONG});
            return;
        }

        var targetIndex = parseInt(msg.targetIndex);
        var targetPlayer = this.getPlayerByIndex(targetIndex);
        if (!targetPlayer) {
            callback({code: SubCode.INDEX_INVALID});
            return;
        }

        if (!player.checkResource(giftConfig.costs)) {
            callback({code: Code.LACK_OF_COIN});
            return;
        }

        this.broadcast("onPlayerGift", {
            playerIndex: player.getIndex(),
            targetIndex: targetIndex,
            type: giftType
        });
        callback({});

        player.costResourceSync(giftConfig.costs);
        // targetPlayer.addResourceSync(giftConfig.harvest);
    },*/
    handleJiaoDiZhu: function(player, msg, callback){
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING});
            return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING});
            return;
        }
        if(this._curStatus != Const.Status.JIAO){
            callback({code: SubCode.LXE_STATE_ERROR});
            return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY});
            return;
        }
        if(!msg.hasOwnProperty('flag')){
            callback({code: Code.ARGS_INVALID});
            return;
        }

        callback({});
        if(msg.flag){
            this._diZhu = player.getIndex();
            player._isDiZhu = true;
            player._pass = true;
            player._jiaoStatus = Const.PlayerStatus.JIAO;
            this._roundTurns = 0;
            this.onPlayerJiaoDiZhu();
            /*this.broadcast("onPlayerJiaoDiZhu",{
             playerIndex: player.getIndex(),
             flag: msg.flag
             });*/
            this.broadcast("onPlayerStatusChange",{
                playerIndex: player.getIndex(),
                status: player._jiaoStatus
            });
            var dz = this.getPlayerByIndex(this._diZhu);
            dz._curRate = 0;
            this.enumPlaying(function(eIndex, ePlayer){
                if(eIndex == this._diZhu){
                    return;
                }
                dz._curRate += ePlayer._curRate;
            }.bind(this));

            var players = {};
            this.enumPlaying(function(eIndex, ePlayer){
                players[eIndex] = {};
                players[eIndex].curRate = ePlayer._curRate;
            }.bind(this));
            this.broadcast("onPlayerCurRateChange",{
                players: players
            });
            this._curStatus = Const.Status.TI;
            var nextPlayer = this.getPlayerByIndex(this.getNextPlaying(this._diZhu));
            var partner = this.getPlayerByIndex(nextPlayer._partner);
            if(nextPlayer._tiStat < 0){
                this._changeCurPlay(nextPlayer.getIndex());
            }else if(partner._tiStat < 0){
                this._changeCurPlay(partner.getIndex());
            }else{
                this._curStatus = Const.Status.CHU;
                this.startPlay();
            }
        }else{
            player._jiaoStatus = Const.PlayerStatus.BUJIAO;
            player._tiStat = 0;
            /*this.broadcast("onPlayerJiaoDiZhu",{
             playerIndex: player.getIndex(),
             flag: msg.flag
             });*/
            this.broadcast("onPlayerStatusChange",{
                playerIndex: player.getIndex(),
                status: player._jiaoStatus
            });
            var next = this.getNextPlaying(this._curPlay);

            if(next == this._dealerIndex){

                this._doStartNewRound();
                return;
            }
            this._changeCurPlay();
        }

    },
    handleJiaoDiZhu2: function(player, msg, callback){
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING});
            return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING});
            return;
        }
        if(this._curStatus != Const.Status.JIAO){
            callback({code: SubCode.LXE_STATE_ERROR});
            return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY});
            return;
        }
        if(this.getSubType() != 'DDZ'){
            callback({code: SubCode.LXE_MS_UNMATCH});
            return;
        }
        if(msg.hasOwnProperty('score')){
            if(isNaN(msg.score)) {
                callback({code: Code.ARGS_INVALID});
                return;
            }else {
                if(msg.score < 0 || msg.score > 3){
                    callback({code: Code.ARGS_VALUE_WRONG});
                    return;
                }
            }
        }else {
            callback({code: Code.ARGS_INVALID});
            return;
        }
        player._pass = true;
        if(msg.score > this._roundRate){
            this._roundRate = msg.score;
        }else if(msg.score == 0){
        }else {
            callback({code: Code.ARGS_INVALID});
            return;
        }
        callback({});
        player._jiaoDiZhu = msg.score;
        if(msg.score){
            player._jiaoStatus = msg.score;
            this.broadcast("onPlayerStatusChange",{
                playerIndex: player.getIndex(),
                status: player._jiaoStatus
            });

        }else{
            player._jiaoStatus = Const.PlayerStatus.BUJIAO;
            /*this.broadcast("onPlayerJiaoDiZhu",{
                playerIndex: player.getIndex(),
                flag: msg.flag
            });*/
            this.broadcast("onPlayerStatusChange",{
                playerIndex: player.getIndex(),
                status: player._jiaoStatus
            });

            /*var next = this.getNextPlaying(this._curPlay);
            if(next == this._dealerIndex){

                this._doStartNewRound();
                return;
            }*/
        }
        var allSelect = true;
        var maxScore = 0;
        var maxIndex = 0;
        this.enumPlaying(function(eIndex, ePlayer) {
            if(ePlayer.isScoreSelected()){
                if(ePlayer._jiaoDiZhu > maxScore){
                    maxScore = ePlayer._jiaoDiZhu;
                    maxIndex = eIndex;
                }
            }else {
                allSelect = false;
            }
        });
        if(allSelect || maxScore == 3){
            if(maxIndex == 0){
                this._doStartNewRound();
                return;
            }
            this.setDiZhu(maxIndex);
            this._roundRate = maxScore;
            var players = {};
            this.enumPlaying(function(eIndex, ePlayer){
                players[eIndex] = {};
                players[eIndex].curRate = (this._bombNum + 1);
            }.bind(this));
            this.broadcast("onPlayerCurRateChange",{
                players: players
            });
            this._curStatus = Const.Status.CHU;

            this.startPlay();
        }else{
            this._changeCurPlay();
        }
    },
    setDiZhu: function (index){
        var player = this.getPlayerByIndex(index);
        this._diZhu = player.getIndex();
        player._isDiZhu = true;
        player._pass = true;
        this.broadcast("onDiZhu",{
            playerIndex: player.getIndex(),
        });
        this.onPlayerJiaoDiZhu();

    },
    handleTiCard: function(player, msg, callback){
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING});
            return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING});
            return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY});
            return;
        }
        if(this._curStatus == Const.Status.TI) {
            if (player.isTiSelected()) {
                callback({code: SubCode.LXE_PLAYER_IS_TI});
                return;
            }
        }
        if(!msg.hasOwnProperty('flag')){
            callback({code: Code.ARGS_INVALID});
            return;
        }
        callback({});
        if(this._curStatus == Const.Status.TI){
            if(msg.flag){
                player._jiaoStatus = Const.PlayerStatus.TI;
                player._roundTi = Const.PlayerStatus.TI;
                player._tiCard = 1;
                player._tiStat = 1;
                player._curRate *= 2;
                /*this.broadcast("onPlayerTi",{
                    playerIndex: player.getIndex(),
                    flag: msg.flag
                    //curRate: this.getCurRate()
                });*/
                this.broadcast("onPlayerStatusChange",{
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                var partner = this.getPlayerByIndex(player._partner);
                if(partner._tiStat < 0){
                    this._curStatus = Const.Status.GEN;
                    this._changeCurPlay(partner.getIndex());
                }else if(partner._tiStat == 0){
                    this._curStatus = Const.Status.GEN;
                    this._changeCurPlay(partner.getIndex());
                }else {
                    this._curStatus = Const.Status.HUI;
                    this._changeCurPlay(this._diZhu);
                }
            }else {
                player._tiStat = 0;
                player._tiCard = 0;
                player._jiaoStatus = Const.PlayerStatus.BUTI;
                player._roundTi = Const.PlayerStatus.BUTI;

                /*this.broadcast("onPlayerTi",{
                    playerIndex: player.getIndex(),
                    flag: msg.flag
                    //curRate: this.getCurRate()
                });*/
                this.broadcast("onPlayerStatusChange",{
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                var partner = this.getPlayerByIndex(player._partner);
                if(partner._tiStat < 0){
                    this._curStatus = Const.Status.TI;
                    this._changeCurPlay(partner.getIndex());
                }else{
                    this._curStatus = Const.Status.CHU;
                    this.startPlay();
                }
            }

        }else if(this._curStatus == Const.Status.GEN){
            if(msg.flag){
                player._tiCard = 1;
                player._tiStat = 1;
                player._jiaoStatus = Const.PlayerStatus.GEN;
                player._roundTi = Const.PlayerStatus.GEN;

                player._curRate *= 2;
                /*this.broadcast("onPlayerGen",{
                    playerIndex: player.getIndex(),
                    flag: msg.flag
                });*/
                this.broadcast("onPlayerStatusChange",{
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });

                this._curStatus = Const.Status.HUI;
                this._changeCurPlay(this._diZhu);
            }else {
                player._tiStat = 0;
                player._tiCard = 0;
                player._jiaoStatus = Const.PlayerStatus.BUGEN;
                player._roundTi = Const.PlayerStatus.BUGEN;

                this.broadcast("onPlayerStatusChange",{
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                /*this.broadcast("onPlayerGen",{
                    playerIndex: player.getIndex(),
                    flag: msg.flag
                });*/
                this._curStatus = Const.Status.HUI;
                this._changeCurPlay(this._diZhu);
            }

        }else if(this._curStatus == Const.Status.HUI){
            if(msg.flag){
                player._jiaoStatus = Const.PlayerStatus.HUI;
                player._roundTi = Const.PlayerStatus.HUI;

                player._tiCard = 1;
                player._tiStat = 1;
                this.enumPlayers(function(eIndex, ePlayer) {
                    if(eIndex == this._diZhu){
                        return;
                    }
                    if(ePlayer.isTiCard()){
                        ePlayer._curRate *= 2;
                    }
                });
                /*this.broadcast("onPlayerHui",{
                    playerIndex: player.getIndex(),
                    flag: msg.flag
                    //curRate: this.getCurRate()
                });*/
                this.broadcast("onPlayerStatusChange",{
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });

                this._curStatus = Const.Status.CHU;
                this.startPlay();
            }else {
                player._tiStat = 0;
                player._tiCard = 0;
                player._jiaoStatus = Const.PlayerStatus.BUHUI;
                player._roundTi = Const.PlayerStatus.BUHUI;

                /*this.broadcast("onPlayerHui",{
                    playerIndex: player.getIndex(),
                    flag: msg.flag
                    //curRate: this.getCurRate()
                });*/
                this.broadcast("onPlayerStatusChange",{
                    playerIndex: player.getIndex(),
                    status: player._jiaoStatus
                });
                this._curStatus = Const.Status.CHU;
                this.startPlay();
            }
        }
        var dz = this.getPlayerByIndex(this._diZhu);
        dz._curRate = 0;
        this.enumPlaying(function(eIndex, ePlayer){
            if(eIndex == this._diZhu){
                return;
            }
            dz._curRate += ePlayer._curRate;
        }.bind(this));

        var players = {};
        this.enumPlaying(function(eIndex, ePlayer){
            players[eIndex] = {};
            players[eIndex].curRate = ePlayer._curRate;
        }.bind(this));
        this.broadcast("onPlayerCurRateChange",{
            players: players
        });

    },
    onBeforePlayCard: function(player, msg, cards){
        var pattern = msg.pattern;
        if(!pattern){
            pattern = this.getPattern(cards);
            msg.pattern = pattern;
            if(!pattern){
                return SubCode.PATTERN_ERROR;
            }
        }
        var code = 0;

        //检测玩家手中是否有这些牌
        var handCards = player._handCards.getCards();
        ExObject.eachKeyNum(cards, function(c, v){
            if(!handCards.hasOwnProperty(c)) {
                code = SubCode.CARD_NOT_EXIST;
                return false;
            }
        });
        if(code){
            return code;
        }
        //检测牌型与牌是否符合
        if(player.checkPattern(pattern, cards) == false){
            return SubCode.PATTERN_ERROR;
        }
        console.log("pattern = " + pattern);
        //比牌
        if(player.getIndex() == this._curCardsInfo.playerIndex){
            return 0;
        }
        if(pattern == this._curCardsInfo.pattern){
            //var maxCard = player.getMaxCard(cards, pattern);
            if(ExObject.count(cards) == this._curCardsInfo.cardsNum){
                if( player.getMaxCard(cards, pattern) > this._curCardsInfo.maxCard){
                    return 0;
                }else{
                    return SubCode.LXE_CARDS_IS_SMALL;
                }
            }else {
                return SubCode.LXE_CARDS_IS_SMALL;

            }

        }else if(pattern == Const.Pattern.SPEC_BOMB){
            if(pattern > this._curCardsInfo.pattern){
                return 0;
            }else{
                return SubCode.LXE_CARDS_IS_SMALL;
            }
        }else if(pattern == Const.Pattern.BOMB){
            if(pattern > this._curCardsInfo.pattern){
                return 0;
            }else{
                return SubCode.LXE_CARDS_IS_SMALL;
            }
        }else if(pattern == Const.Pattern.KING_BOMB){
            if(pattern > this._curCardsInfo.pattern){
                return 0;
            }else{
                return SubCode.LXE_CARDS_IS_SMALL;
            }
        }else {
            code = SubCode.PATTERN_ERROR;
        }
        return code;
    },
    _getObj: function(cards){
        var obj = {};
        for(var i = 0; i < cards.length; i++){
            ExObject.addNumber0(obj,cards[i].originValue,1);
        }
        return obj;
    },
    _cardObjToArray: function (arrayObj) {
        var keys = Object.keys(arrayObj);
        var temp = [];
        keys.forEach(function (v) {
            if (arrayObj[v]) {
                temp.push(v);
            }
        });
        return temp;
    },
    handlePlayCard: function(player, msg, callback){
        if (!this.isPlaying()) {
            callback({code: Code.ROOM_NOT_PLAYING}); return;
        }
        if (!player.isPlaying()) {
            callback({code: Code.PLAYER_NOT_PLAYING}); return;
        }
        if (player.getIndex() != this._curPlay) {
            callback({code: SubCode.NOT_CUR_PLAY}); return;
        }
        if(this._curStatus != Const.Status.CHU){
            callback({code: SubCode.LXE_CANT_PLAY_CARDS}); return;
        }
        if(!msg.hasOwnProperty('cards')){
            callback({code: Code.ARGS_INVALID}); return;
        }
        logger.debug("room=%d onPlayCard %j,msg %j", this.rid(), this._curCardsInfo, msg);
        var cards = msg.cards;
        cards = this._getObj(cards);
        var code = this.onBeforePlayCard(player, msg, cards);
        if (code) {
            callback({code: code}); return;
        }
        if(this.getSubType() == 'LFDDZ'){
            if(msg.pattern == Const.Pattern.TRIPLE){
                if(player.getHandCardNum() != 3){
                    code = SubCode.PATTERN_ERROR;
                    callback({code: code}); return;
                }
            }
        }
        callback({});
        this.saveInfo(msg, cards, player);
        var msgInfo = {
            cards: msg.cards,
            pattern: msg.pattern
        };
        this._curCardsInfo.lastPlayCards = msgInfo;
        player._pass = true;
        //将牌从玩家手牌中删除
        player.playCards(cards);
        //广播出牌消息
        var MSG = {
            'playerIndex': player.getIndex(),
            'cards': msg.cards,
            'pattern': msg.pattern
        };
        this.broadcast("onPlayCards", MSG);
        player._jiaoStatus = Const.PlayerStatus.CHU;
        this.broadcast("onPlayerStatusChange",{
            playerIndex: player.getIndex(),
            status: player._jiaoStatus
        });
        //出牌后处理，如果没赢，切换玩家
        if(player._handCards.isEmpty()) {
            if(player._isDiZhu){
                this._diZhuWin = true;
            }
            if(this._diZhuWin){
                var resp = true;
                this.enumPlayers(function(eIndex, ePlayer) {
                    if(eIndex == this._diZhu){
                        return;
                    }
                    //console.log("len = " + ePlayer._stat._roundCards.length + "diz = " + this._diZhu);

                    if(ePlayer._stat._roundCards.length > 0){

                        resp = false;
                        console.log("len = " + ePlayer._stat._roundCards.length + "diz = " + this._diZhu);

                    }

                }.bind(this));
                console.log("resp = " + resp);

                if(resp){
                    this._spring = true;
                    var dz = this.getPlayerByIndex(this._diZhu);
                    dz._curRate = 0;
                    this.enumPlaying(function(eIndex, ePlayer){
                        if(eIndex == this._diZhu){
                            return;
                        }
                        if(this.getSubType() == "LFDDZ"){
                            dz._curRate += ePlayer._curRate;

                        }else {
                            dz._curRate = ePlayer._curRate;

                        }
                    }.bind(this));
                    var players = {};
                    this.enumPlaying(function(eIndex, ePlayer){
                        players[eIndex] = {};
                        players[eIndex].curRate = ePlayer._curRate * 2;
                    }.bind(this));
                    this.broadcast("onPlayerCurRateChange",{
                        players: players
                    });
                }
            }else {
                var diZhu = this.getPlayerByIndex(this._diZhu);
                console.log("len = " + diZhu._stat._roundCards.length);
                if(diZhu._stat._roundCards.length <= 1){
                    this._spring = true;
                    var dz = this.getPlayerByIndex(this._diZhu);
                    dz._curRate = 0;
                    this.enumPlaying(function(eIndex, ePlayer){
                        if(eIndex == this._diZhu){
                            return;
                        }
                        if(this.getSubType() == "LFDDZ"){
                            dz._curRate += ePlayer._curRate;

                        }else {
                            dz._curRate = ePlayer._curRate;

                        }
                    }.bind(this));
                    var players = {};
                    this.enumPlaying(function(eIndex, ePlayer){
                        players[eIndex] = {};
                        players[eIndex].curRate = ePlayer._curRate * 2;
                    }.bind(this));
                    this.broadcast("onPlayerCurRateChange",{
                        players: players
                    });
                }
            }

            this.settlementCurRound(0);
        }else {
            this._changeCurPlay();
        }
    },
    getCardsNum: function(cards){
        return ExObject.sumValue(cards);
    },

    saveInfo: function(msg,cards,player) {
        this._curCardsInfo.pattern = msg.pattern;
        this._curCardsInfo.cards = cards;
        this._curCardsInfo.playerIndex = player.getIndex();
        this._curCardsInfo.maxCard = player.getMaxCard(cards, msg.pattern);
        this._curCardsInfo.cardsNum = this.getCardsNum(cards);
        var tempCards = this._cardObjToArray(cards);

        player._lastCards = {
            pass: 1,
            cards: tempCards,
            pattern: msg.pattern
        };
        player._stat._roundCards.push(tempCards);
        if (msg.pattern == Const.Pattern.SPEC_BOMB || msg.pattern == Const.Pattern.BOMB || msg.pattern == Const.Pattern.KING_BOMB) {
            var maxRate = this.getOption("maxRate");
            player._bomb += 1;
            if (maxRate) {
                if (this._bombNum < maxRate) {
                    this._bombNum += 1;
                    if (this._curCardsInfo.maxCard == 1 && this._curCardsInfo.cardsNum == 4 && this.getSubType() == 'LFDDZ') {
                        //this._bombNum += 2;
                        for (var i = 0; i < 3; i++) {
                            this.enumPlayers(function(eIndex, ePlayer) {
                                if(eIndex == this._diZhu){
                                    return;
                                }
                                ePlayer._curRate *= 2;
                            }.bind(this));
                        }
                    } else {
                        this.enumPlayers(function (eIndex, ePlayer) {
                            if (eIndex == this._diZhu) {
                                return;
                            }
                            ePlayer._curRate *= 2;
                        }.bind(this));
                    }
                    var dz = this.getPlayerByIndex(this._diZhu);
                    dz._curRate = 0;
                    this.enumPlaying(function(eIndex, ePlayer){
                        if(eIndex == this._diZhu){
                            return;
                        }
                        if(this.getSubType() == "LFDDZ"){
                            dz._curRate += ePlayer._curRate;

                        }else {
                            dz._curRate = ePlayer._curRate;

                        }
                    }.bind(this));
                    var players = {};
                    this.enumPlaying(function(eIndex, ePlayer){
                        players[eIndex] = {};
                        players[eIndex].curRate = ePlayer._curRate;
                    }.bind(this));
                    this.broadcast("onPlayerCurRateChange",{
                        players: players
                    });
                }
            }else {
                this._bombNum += 1;
                if (this._curCardsInfo.maxCard == 1 && this._curCardsInfo.cardsNum == 4 && this.getSubType() == 'LFDDZ') {
                    //this._bombNum += 2;
                    for (var i = 0; i < 3; i++) {
                        this.enumPlayers(function(eIndex, ePlayer) {
                            if(eIndex == this._diZhu){
                                return;
                            }
                            ePlayer._curRate *= 2;
                        }.bind(this));
                    }
                } else {
                    this.enumPlayers(function (eIndex, ePlayer) {
                        if (eIndex == this._diZhu) {
                            return;
                        }
                        ePlayer._curRate *= 2;
                    }.bind(this));
                }
                var dz = this.getPlayerByIndex(this._diZhu);
                dz._curRate = 0;
                this.enumPlaying(function(eIndex, ePlayer){
                    if(eIndex == this._diZhu){
                        return;
                    }
                    if(this.getSubType() == "LFDDZ"){
                        dz._curRate += ePlayer._curRate;

                    }else {
                        dz._curRate = ePlayer._curRate;

                    }
                }.bind(this));
                var players = {};
                this.enumPlaying(function(eIndex, ePlayer){
                    players[eIndex] = {};
                    players[eIndex].curRate = ePlayer._curRate;
                }.bind(this));
                this.broadcast("onPlayerCurRateChange",{
                    players: players
                });
            }
        }
    },

    onSettlement: function() {
        this._super();
        var room = this;
        /*this.enumPlayers(function(eIndex, ePlayer) {
            if(eIndex == room._diZhu){
                ePlayer.settlementPattern();
            }
        });*/
        var subConfig = room.getSubConfig();
        var di = subConfig.options.BASE;



        /*this.enumPlayers(function(eIndex, ePlayer) {
            if(ePlayer._tiCard == 1){
                pow += 1;
            }
        });*/
        var diZhu = this.getPlayerByIndex(this._diZhu);

        this.enumPlayers(function(eIndex, ePlayer) {
            if(eIndex == room._diZhu){
                return;
            }
            var score = 0;
            if(room.getSubType() == "LFDDZ"){
                score = ePlayer._curRate * di;
            }else{
                score = ePlayer._curRate * di * this._roundRate;
            }

            if(room._spring){
                console.log("spring = " + room._spring);
                score *= 2;
            }
            if(room._diZhuWin){
                diZhu.addRoundScore(score);
                ePlayer.addRoundScore(-score);
            }else{
                diZhu.addRoundScore(-score);
                ePlayer.addRoundScore(score);
            }
        }.bind(this));

    },
    getPattern: function(cards){
        var patterns = Const.Pattern;
        var player = this.getCurPlayer();
        var pattern = 0;
        ExObject.each(patterns, function(p, v){
            if(pattern){
                return;
            }
            if(player.checkPattern(v, cards)){
                pattern = v;
            }
        });
        return pattern;
    },
    getSettlementInfo: function(){
        var info = this._super();
        var player = this.getPlayerByIndex(this.RoundWinPlayer);
        info.winIndex = this.RoundWinPlayer;
        info.diZhuCards = this._diZhuCards;
        info.diZhu = this._diZhu;
        info.spring = this._spring;
        return info;
    },

    onCurRoundFinished: function() {
        this._super();
        this._dealerIndex = this.getNextIndex(this._dealerIndex);
        this._curStatus = Const.Status.NONE;
        this._diZhu = 0;
        this.enumPlaying(function(eIndex, ePlayer){
            ePlayer._lastCards = {
                pass: -1
            };
        }.bind(this));
        this._roundStatus = RoundStatus.WAITING;
    },

    /******************************************************************
     * 功能接口
     ******************************************************************/
    _changeCurPlay: function(playerIndex) {
        if(playerIndex){
            this._curPlay = playerIndex;
        }else {
            this._curPlay = this.getNextIndex(this._curPlay);
        }
        var player = this.getPlayerByIndex(this._curPlay);
        if(player._jiaoStatus > 0){
            player._jiaoStatus = Const.PlayerStatus.NONE;
        }
        if(this._curStatus == Const.Status.CHU){
            console.log("card = %d cccccccccccccccccc cur = %d", this._curCardsInfo.playerIndex, this._curPlay);
            if(this._curPlay == this._curCardsInfo.playerIndex){
                player._pass = false;
                this._roundTurns += 1;
                this.broadcast("onNewTurns",{
                    turns: this._roundTurns
                });
                this._curCardsInfo.lastPlayCards = {
                    cards: [],
                    pattern: 0
                };
                this.enumPlaying(function(eIndex, ePlayer){
                    ePlayer._jiaoStatus = Const.PlayerStatus.NONE;
                    ePlayer._lastCards = {
                        pass: -1
                    };
                }.bind(this))
            }
        }else if(this._curStatus == Const.Status.JIAO){
            if(this.getSubType() == 'LFDDZ'){
                if(player.haveBomb()){
                    player._pass = false;
                }
            }
        }
        this._sendCurPlay(player._pass);
    },

    _startNewRoundAfter: function(timeMS) {
        if (this._roundStatus != RoundStatus.WAITING) {
            return;
        }

        this.broadcast("onRoomStartAfter", {
            time: timeMS,
        });

        this._roundStatus = RoundStatus.PREPARE;

        setTimeout(function() {
            if (this._roundStatus != RoundStatus.PREPARE) {
                return;
            }

            this._roundStatus = RoundStatus.READY;
            this.tryStartNewRound();
        }.bind(this), timeMS);
    },

    /**
     * 广播当前出牌玩家
     * @private
     */
    _sendCurPlay: function(pass) {
        var msg = {
            'curPlay': this._curPlay,
            'pass': true,
            'status': this._curStatus
        };
        if(!pass){
            msg.pass = false;
        }
        this.broadcastReplay("onCurPlay", msg);
    },
});